package com.zzz.gp.controller;

import com.alibaba.fastjson.JSONObject;
import com.zzz.gp.bean.GpHistory;
import com.zzz.gp.service.ExcelExportService;
import com.zzz.gp.utils.ExcelExportUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/12/9 20:44
 */
@RestController
@RequestMapping("/zzz")
public class ExcelExportController {
    @Autowired
    public ExcelExportService service;

    /**
     * 接口调用量数据导出
     *
     * @name: export
     * @return: void
     * @auther: ZZZ
     * @date: 2021/10/9 14:56
     */
    @RequestMapping(path="/export/{code}",method = RequestMethod.GET)
    public String export(@PathVariable String code, HttpServletResponse response)  {


        //查询业务数据
        List<GpHistory> result = service.export(code);
        List<Map<String, Object>> list = JSONObject.parseObject(JSONObject.toJSONString(result), List.class);

        //表头数组
        String[] lables = new String[]{"股票代码", "股票名称", "今开", "收盘", "涨幅", "涨幅百分比", "最低", "最高","成交量","金额","换手率","日期"};
        //查询数据对应的属性数组
        String[] fields = new String[]{"code", "name", "open", "close", "increase", "increase_p", "low", "high","volume","money","rate","day"};
        String title = code+"历史数据.xlsx";
        String headTitle = code+"历史数据";
        if(result.size()>1){
             title = result.get(0).getName()+"("+result.get(0).getCode()+")"+"历史数据.xlsx";
             headTitle = result.get(0).getName()+"("+result.get(0).getCode()+")"+"历史数据";
        }
        ExcelExportUtil.titleExport(response, list, lables, fields, title, headTitle);
        //ExcelExportUtil.export(response, list, lables, fields, title);
        return headTitle;

    }
}
